/*
Copyright (C) 2014  Johan Degraeve

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.

*/package helpdiabetesj2me;

import javax.microedition.lcdui.*;

/**
 *
 * @author Johan
 */
public class UnitList extends List implements CommandListener {
    private Displayable curdisp;
    AddItem localreference;
    
     static String[] str_List_title = {"Kies Eenheid :"};
     
    public UnitList() {
        super(str_List_title[HelpDiabetes.languageIndex],List.IMPLICIT);
        this.addCommand(List.SELECT_COMMAND);
        this.setCommandListener(this);
    }
      
    // first deletes all units that may still be there
    // then adds the units to the list    
    // switch the display to this list
    // AddItem is used here to allow getting the current unit number and to change the value afterwards, and to recall 'additem.selectedItem'
    public void selectUnit(AddItem callingobject, int nr_of_units, String [] unitdescription) {
        deleteAll();
        for (int cntr = 0;cntr < nr_of_units; cntr++) {
            append(unitdescription[cntr],null);
        }
    
        localreference = callingobject;
        setSelectedIndex(callingobject.get_unit_nubmer_to_add() - 1,true);
        
        curdisp = callingobject;
        HelpDiabetes.midlet.getDisplay().setCurrent(this);
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if (command == List.SELECT_COMMAND) {
            boolean localtemp;
            if (localreference.get_rec_to_change() == 0) {
                localtemp = true;
            } else {
                localtemp = false;
            }

              int standard_amount = localreference.CalculateStandardAmount_in_b(getSelectedIndex()+1);
              localreference.ItemSelectedWithoutStoringCurdisp(localreference.get_chosen_record_id(), localtemp, standard_amount == 100 ? "0.0" : (new Float((long)standard_amount)).toString(), localreference.get_rec_to_change(), getSelectedIndex()+1);
              
              HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
        }
    }
    
}
